package cn.tedu.bonfire.admin.content.service.impl;


import cn.tedu.bonfire.admin.content.dao.persist.repository.IArticleRepository;
import cn.tedu.bonfire.admin.content.dao.persist.repository.ICategoryRepository;
import cn.tedu.bonfire.admin.content.pojo.dto.CategoryAddNewDto;
import cn.tedu.bonfire.admin.content.pojo.dto.CategoryUpdateInfoDto;
import cn.tedu.bonfire.admin.content.pojo.entity.Category;
import cn.tedu.bonfire.admin.content.pojo.vo.CategoryListItemVO;
import cn.tedu.bonfire.admin.content.pojo.vo.CategoryStandardVO;
import cn.tedu.bonfire.admin.content.pojo.vo.TypeListItemVO;
import cn.tedu.bonfire.admin.content.service.ICategoryService;
import cn.tedu.bonfire.common.enumerator.ServiceCode;
import cn.tedu.bonfire.common.ex.ServiceException;
import cn.tedu.bonfire.common.pojo.vo.PageData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class CategoryServiceImpl implements ICategoryService {
    @Autowired
    private ICategoryRepository categoryRepository;
    @Value("${bonfire.dao.default-query-page-size}")
    private Integer defaultQueryPageSize;

    @Override
    public CategoryStandardVO getStandardById(Long id){
        CategoryStandardVO queryResult= categoryRepository.getStandardById(id);
        if (queryResult == null) {
            String message = "查询类别详情失败，尝试访问的数据不存在！";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return queryResult;
    }

    @Override
    public PageData<CategoryListItemVO> listByParent(Long patentId, Integer pageNum, Integer pageSize) {
        return categoryRepository.listByParent(patentId, pageNum, pageSize);
    }

    @Override
    public void addNew(CategoryAddNewDto categoryAddNewDto) {
        String name = categoryAddNewDto.getName();
        int count = Math.toIntExact(categoryRepository.countByName(name));
        if (count > 0) {
            String message = "添加类别失败，类别名称已经被占用！";
            log.warn(message);

            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }

        Category category = new Category();
        BeanUtils.copyProperties(categoryAddNewDto, category);
        int rows = categoryRepository.insert(category);
        if (rows != 1) {
            String message = "添加类别失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【根据ID删除类别】的业务，参数：{}", id);
        CategoryStandardVO currentCategory = categoryRepository.getStandardById(id);
        if (currentCategory == null) {
            String message = "删除类别失败，尝试删除的类别数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }



        int rows = categoryRepository.deleteById(id);
        if (rows != 1) {
            String message = "删除类别失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public void updateInfoById(Long id, CategoryUpdateInfoDto categoryUpdateInfoParam) {
        log.debug("开始处理【修改类别详情】的业务，ID：{}，新数据：{}", id, categoryUpdateInfoParam);
        CategoryStandardVO queryResult = categoryRepository.getStandardById(id);
        if (queryResult == null) {
            String message = "修改类别详情失败，尝试修改的类别数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        long count = categoryRepository.countByNameAndNotId(id, categoryUpdateInfoParam.getName());
        if (count > 0) {
            String message = "修改类别详情失败，类别名称已经被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }

        Category category = new Category();
        BeanUtils.copyProperties(categoryUpdateInfoParam, category);
        category.setId(id);
        int rows = categoryRepository.updateById(category);
        if (rows != 1) {
            String message = "修改类别详情失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    @Override
    public PageData<TypeListItemVO> listType(Integer pageNum, Integer pageSize) {
        return categoryRepository.listType(pageNum, pageSize);

    }


}
